package com.xiao.sulingwallet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import com.xiao.sulingwallet.entity.PurseEntity;
import com.xiao.sulingwallet.service.PurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;



/**
 * 钱包表
 *
 * @author xiao
 * @email 1322127758@qq.com
 * @date 2022-03-30 17:57:48
 */
@RestController
@RequestMapping("sulingwallet/purse")
public class PurseController {
    @Autowired
    private PurseService purseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulingwallet:purse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{puId}")
    //@RequiresPermissions("sulingwallet:purse:info")
    public R info(@PathVariable("puId") Long puId){
		PurseEntity purse = purseService.getById(puId);

        return R.ok().put("purse", purse);
    }

    /**
     * 通过用户号获取数据
     */
    @RequestMapping("/info/balance")
    public R getBalance(@RequestParam("userName")String userName){
        PurseEntity purse = purseService.getOne(new QueryWrapper<PurseEntity>().eq("pu_member_id", userName));
        if(purse == null){
            purse = new PurseEntity();
            purse.setPuMemberId(userName);
            purse.setPuAmount(new BigDecimal("0"));
            purseService.save(purse);
        }

        return R.ok().put("purse", purse);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulingwallet:purse:save")
    public R save(@RequestBody PurseEntity purse){
		purseService.save(purse);

        return R.ok();
    }

    /**
     * 通过用户号,修改钱包金额
     * @param companyUserName :要从哪个号转出
     * @param userName :要转到这个用户号
     * @param amount :金额
     * @return
     */
    @RequestMapping("/update/amount")
    public R updateAmount(@RequestParam("companyUserName")String companyUserName, @RequestParam("userName")String userName, @RequestParam("amount") BigDecimal amount) throws Exception {

        return purseService.updateAmount(companyUserName, userName, amount);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulingwallet:purse:update")
    public R update(@RequestBody PurseEntity purse){
		purseService.updateById(purse);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulingwallet:purse:delete")
    public R delete(@RequestBody Long[] puIds){
		purseService.removeByIds(Arrays.asList(puIds));

        return R.ok();
    }

}
